﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 
    /// </summary>
    public class LineStyle : Entity
    {
        #region API 函数
        /// <summary>
        /// 名字
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="name"><see cref="StringRef"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleGetName(IntPtr line_style, ref IntPtr name);

        /// <summary>
        /// 获取像素宽度
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="pixel_width"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleGetWidthPixels(IntPtr line_style, out double pixel_width);

        /// <summary>
        /// 设置像素宽度
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="pixel_width"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleSetWidthPixels(IntPtr line_style, double pixel_width);

        /// <summary>
        /// 获取长度系数
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="length_multiplier"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleGetLengthMultiplier(IntPtr line_style, out double length_multiplier);

        /// <summary>
        /// 设置长度系数
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="length_multiplier"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleSetLengthMultiplier(IntPtr line_style, double length_multiplier);

        /// <summary>
        /// 获取颜色
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleGetColor(IntPtr line_style, ref Color color);

        /// <summary>
        /// 设置颜色
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleSetColor(IntPtr line_style, Color color);

        /// <summary>
        /// 从另一个线型复制一个新线型
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="name"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleCreateCopy(IntPtr line_style, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, out IntPtr destination);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="line_style"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleRelease(ref IntPtr line_style);

        /// <summary>
        /// 是否是用户创建的
        /// </summary>
        /// <param name="line_style"></param>
        /// <param name="is_user_created"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SULineStyleIsUserCreated(IntPtr line_style, out bool is_user_created);
        #endregion

        /// <summary>
        /// 是否是为用户创建的
        /// </summary>
        public bool IsUserCreated
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleIsUserCreated(Id, out bool b));
                return b;
            }
        }

        /// <summary>
        /// 线型的名字
        /// </summary>
        public string Name
        {
            get
            {
                StringRef s = StringRef.Empty;
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleGetName(Id, ref s.Id));
                return s.Dispose();
            }
        }

        /// <summary>
        /// 像素宽度
        /// </summary>
        public double PixelWidth
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleGetWidthPixels(Id, out double w));
                return w;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleSetWidthPixels(Id, value));
            }
        }

        /// <summary>
        /// 长度系数
        /// </summary>
        public double LengthMultiplier
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleGetLengthMultiplier(Id, out double multiplier));
                return multiplier;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleSetLengthMultiplier(Id, value));
            }
        }

        /// <summary>
        /// 颜色
        /// </summary>
        public Color Color
        {
            get
            {
                Color c = new Color();
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleGetColor(Id, ref c));
                return c;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SULineStyleSetColor(Id, value));
            }
        }

        internal LineStyle(IntPtr id) : base(id) { }

        /// <summary>
        /// 复制线型
        /// </summary>
        /// <param name="name">新线型的名字</param>
        /// <returns></returns>
        public LineStyle Clone(string name)
        {
            SketchUpExceptionHelper.ThrowSUResult(SULineStyleCreateCopy(Id, name, out IntPtr d));
            return new LineStyle(d);
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SULineStyleRelease(ref Id));
        }
    }
}
